//
//  XLHTTPSessionManager+Login.m
//  XiangLove
//
//  Created by 陈宪栋 on 2017/11/28.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLHTTPSessionManager+Login.h"
#import <NSString+DeviceId.h>
#import "XLUserInfoModel.h"
#import <AdSupport/AdSupport.h>



@implementation XLHTTPSessionManager (Login)
- (RACSignal *)visitorLogin {
    NSString *URLString = @"/Api/v2/Login/DeviceLogin";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSString *deviceId = [NSString staticUUID];
    [parameters setObject:deviceId ?: @""   forKey:@"deviceId"];
    [parameters setObject:QudaoName    forKey:@"qudao"];
    [parameters setObject:@"9"              forKey:@"appId"];
    [parameters setObject:[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString] forKey:@"idfa"];
    NSString *sign = [[F(@"xinliao_api%@", deviceId) md5] uppercaseString];
    [self.requestSerializer setValue:sign ?: @"" forHTTPHeaderField:@"sign"];
    return [self XLV2GET:URLString parameters:parameters resultClass:[XLUserInfoModel class]];
}

- (RACSignal *)userLoginWithAppUid:(NSString *)appUid wxOpenId:(NSString *)wxOpenId userName:(NSString *)userName headImgUrl:(NSString *)headImgUrl sex:(UserSex)sex age:(NSInteger)age boundSource:(BoundSourceType)boundSource {
    NSString *URLString = @"/api/v2/login/WeChatLogin";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"9"                              forKey:@"AppLoginType"];
    [parameters setObject:appUid ?: @""                     forKey:@"AppuId"];
    [parameters setObject:wxOpenId ?: @""                   forKey:@"WxOpenId"];
    [parameters setObject:[UserDefaults objectForKey:OPENINSTALLDATA] ?: @""                   forKey:@"superior"];
    [parameters setObject:userName ?: @""                   forKey:@"UserName"];
    [parameters setObject:[NSString getIpAddresses] ?: @""  forKey:@"IP"];
    [parameters setObject:headImgUrl ?: @""                 forKey:@"HeadImgUrl"];
    [parameters setObject:@(sex)                            forKey:@"Sex"];
    [parameters setObject:QudaoName                    forKey:@"Qudao"];
    [parameters setObject:[NSString staticUUID] ?: @""      forKey:@"DeviceId"];
    [parameters setObject:@(age)                            forKey:@"Age"];
    [parameters setObject:@(boundSource)                    forKey:@"BoundSource"];
    [parameters setObject:[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString] forKey:@"idfa"];
    NSString *sign = [[F(@"xinliao_api%@", appUid) md5] uppercaseString];
    [self.requestSerializer setValue:sign ?: @"" forHTTPHeaderField:@"sign"];
    return [self XLV2POST:URLString parameters:parameters resultClass:[XLUserInfoModel class]];
}

- (RACSignal *)getCodeWithPhone:(NSString *)phone {
    NSString *URLString = @"/api/v1/Login/UserSendMobile";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSString *deviceId = [NSString staticUUID];
    NSString *sign = [[F(@"uname=%@,key=xiuqu", phone) md5] uppercaseString];
    [parameters setObject:phone ?: @"" forKey:@"mobile"];
    [parameters setObject:deviceId ?: @"" forKey:@"deviceCode"];
    [parameters setObject:sign ?: @"" forKey:@"sign"];
    return [self CODEGET:URLString parameters:parameters resultClass:nil];
}

- (RACSignal *)userLoginWithPhone:(NSString *)phone code:(NSString *)code {
    NSString *URLString = @"/api/v2/Login/UserMobileLogin";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSString *deviceId = [NSString staticUUID];
    NSString *sign = [[F(@"uname=%@,key=xiuqu", phone) md5] uppercaseString];
    [parameters setObject:phone ?: @"" forKey:@"mobileCode"];
    [parameters setObject:code ?: @"" forKey:@"sendcode"];
    [parameters setObject:QudaoName forKey:@"qudao"];
    [parameters setObject:@"1" forKey:@"appId"];
    [parameters setObject:deviceId ?: @"" forKey:@"deviceId"];
    [parameters setObject:sign ?: @"" forKey:@"sign"];
    [parameters setObject:[UserDefaults objectForKey:OPENINSTALLDATA] ?: @""                   forKey:@"superior"];
    [parameters setObject:[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString] forKey:@"idfa"];
    return [self XLV2GET:URLString parameters:parameters resultClass:[XLUserInfoModel class]];
}
- (RACSignal *)userBindWithPhone:(NSString *)phone code:(NSString *)code {
    NSString *URLString = @"/api/v2/login/bindphone";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSString *deviceId = [NSString staticUUID];
    NSString *sign = [[F(@"uname=%@,key=xiuqu", phone) md5] uppercaseString];
    [parameters setObject:phone ?: @"" forKey:@"mobile"];
    [parameters setObject:[[XLAccountManager shareInstance] model].Id forKey:@"userId"];
    [parameters setObject:code ?: @"" forKey:@"sendcode"];
    [parameters setObject:QudaoName forKey:@"qudao"];
    [parameters setObject:@"9" forKey:@"appId"];
    [parameters setObject:deviceId ?: @"" forKey:@"deviceId"];
    [parameters setObject:sign ?: @"" forKey:@"sign"];
    [parameters setObject:[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString] forKey:@"idfa"];
    return [self XLV2POST:URLString parameters:parameters resultClass:[XLUserInfoModel class]];
}
- (RACSignal *)refreshSecretKeyWithUserId:(NSString *)userId {
    NSString *URLString = @"Api/v1/UserInfo/SecretKeyRefresh";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:userId ?: @""     forKey:@"userid"];
    [parameters setObject:QudaoName    forKey:@"qudao"];
    NSString *sign = [[F(@"xinliao_api%@", userId) md5] uppercaseString];
    [self.requestSerializer setValue:sign ?: @"" forHTTPHeaderField:@"sign"];
    return [self GET:URLString parameters:parameters resultClass:[XLUserInfoModel class]];
}
@end
